package com.qlyl.modules.information.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qlyl.common.api.vo.Result;
import com.qlyl.common.aspect.annotation.AutoLog;
import com.qlyl.common.constant.ErrorConstant;
import com.qlyl.common.system.base.controller.JeecgController;
import com.qlyl.common.system.query.QueryGenerator;
import com.qlyl.common.util.oConvertUtils;
import com.qlyl.modules.information.entity.AuthInfo;
import com.qlyl.modules.information.service.IAuthInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;

/**
 * @Description: 认证审核信息
 * @Author: jeecg-boot
 * @Date:   2020-01-09
 * @Version: V1.0
 */
@Slf4j
@Api(tags="认证审核信息")
@RestController
@RequestMapping("/authinfo")
public class AuthInfoController extends JeecgController<AuthInfo, IAuthInfoService> {
	@Autowired
	private IAuthInfoService authInfoService;

	/**
	 * 分页列表查询
	 *
	 * @param authInfo
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "认证审核信息-分页列表查询")
	@ApiOperation(value="认证审核信息-分页列表查询", notes="认证审核信息-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(AuthInfo authInfo,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<AuthInfo> queryWrapper = QueryGenerator.initQueryWrapper(authInfo, req.getParameterMap());
		Page<AuthInfo> page = new Page<AuthInfo>(pageNo, pageSize);
		IPage<AuthInfo> pageList = authInfoService.page(page, queryWrapper);
		return Result.ok(pageList);
	}
	
	/**
	 * 添加
	 *
	 * @param authInfo
	 * @return
	 */
	@AutoLog(value = "认证审核信息-添加")
	@ApiOperation(value="认证审核信息-添加", notes="认证审核信息-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody AuthInfo authInfo) {

		String userid = authInfo.getUserid();
		String comsumsId = authInfo.getComsumsId();


		if(StringUtils.isBlank(userid)||StringUtils.isBlank(comsumsId)){
			return Result.error(ErrorConstant.REQUIREID_PARAMETER_IS_NULL);
		}
		authInfoService.save(authInfo);
		return Result.ok("添加成功！");
	}
	
	/**
	 * 编辑
	 *
	 * @param authInfo
	 * @return
	 */
	@AutoLog(value = "认证审核信息-编辑")
	@ApiOperation(value="认证审核信息-编辑", notes="认证审核信息-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody AuthInfo authInfo) {
		authInfo.setCheckTime(new Date());
		authInfoService.updateById(authInfo);
		return Result.ok("编辑成功!");
	}
	
	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "认证审核信息-通过id删除")
	@ApiOperation(value="认证审核信息-通过id删除", notes="认证审核信息-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		authInfoService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "认证审核信息-批量删除")
	@ApiOperation(value="认证审核信息-批量删除", notes="认证审核信息-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.authInfoService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}



	 /**
	  * 批量审核认证
	  *
	  * @param jsonObject
	  * @return
	  */
	 @RequestMapping(value = "/frozenBatchCheckAuth", method = RequestMethod.PUT)
	 public Result<AuthInfo> frozenBatch(@RequestBody JSONObject jsonObject) {
		 Result<AuthInfo> result = new Result<AuthInfo>();
		 try {
			 String ids = jsonObject.getString("ids");
			 String status = jsonObject.getString("authCheckStatus");
			 String[] arr = ids.split(",");
			 for (String id : arr) {
				 if (oConvertUtils.isNotEmpty(id)) {
					 authInfoService.frozenBatch(id,status);
				 }
			 }
		 } catch (Exception e) {
			 log.error(e.getMessage(), e);
			 result.error500("操作失败" + e.getMessage());
		 }
		 result.success("操作成功!");
		 return result;

	 }
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "认证审核信息-通过id查询")
	@ApiOperation(value="认证审核信息-通过id查询", notes="认证审核信息-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		AuthInfo authInfo = authInfoService.getById(id);
		return Result.ok(authInfo);
	}

  /**
   * 导出excel
   *
   * @param request
   * @param authInfo
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, AuthInfo authInfo) {
      return super.exportXls(request, authInfo, AuthInfo.class, "认证审核信息");
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, AuthInfo.class);
  }

}
